package com.code.springbootpoi.controller;

import com.code.springbootpoi.entity.Person;
import com.code.springbootpoi.entity.Student;
import com.code.springbootpoi.utils.ExcelPOI;
import com.code.springbootpoi.utils.ExcelJXL;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/*
 * @author: liyufei
 * @date: 2018-07-16 16:14
 */
@Slf4j
@Controller
public class ExportExcelController {

    @RequestMapping(value = "/poiExport")
        public void exportExcelByPOI(HttpServletResponse response) {
        List<Person> list = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            Person person = new Person();
            person.setUsername("username" + i);
            person.setPlace("place" + i);
            person.setLegal("legal" + i);
            person.setPhone("phone" + i);
            person.setCode("code" + i);
            person.setDate(1531805953000L);
            person.setResult("result" + i);
            person.setExplain("explain" + i);
            Student student = new Student();
            student.setName("name" + i);
            student.setAge(i);
            person.setStudent(student);
            list.add(person);
        }
        //定义一个 fieldMap ，里面存放的key值要和查询出来的数据字段相对应，里面放的数据也是excel展示的数据
        Map<String, String> fieldMap = new LinkedHashMap<>();
        fieldMap.put("username", "单位名称");
        fieldMap.put("place", "检查地点");
        fieldMap.put("legal", "法定代表人");
        fieldMap.put("phone", "联系电话");
        fieldMap.put("code", "检查单号");
        fieldMap.put("date", "检查日期");
        fieldMap.put("result", "检查结果");
        fieldMap.put("explain", "结果说明");
        fieldMap.put("student.name", "学生名称");
        fieldMap.put("student.age", "学生年龄");
        try {
            ExcelPOI.listToExcel(list, fieldMap, "清单", 60000, response);
        } catch (Exception e) {
            log.error("导出Excel失败。错误信息：" + e.getMessage());
        }
    }

    @RequestMapping(value = "/jxlExport")
    public void exportExcelByJXL(HttpServletResponse response) {
        List<Person> list = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            Person person = new Person();
            person.setUsername("username" + i);
            person.setPlace("place" + i);
            person.setLegal("legal" + i);
            person.setPhone("phone" + i);
            person.setCode("code" + i);
            person.setDate(1531805953000L);
            person.setResult("result" + i);
            person.setExplain("explain" + i);
            list.add(person);
        }
        //定义一个 fieldMap ，里面存放的key值要和查询出来的数据字段相对应，里面放的数据也是excel展示的数据
        Map<String, String> fieldMap = new HashMap<String, String>();
        fieldMap.put("username", "单位名称");
        fieldMap.put("place", "检查地点");
        fieldMap.put("legal", "法定代表人");
        fieldMap.put("phone", "联系电话");
        fieldMap.put("code", "检查单号");
        fieldMap.put("date", "检查日期");
        fieldMap.put("result", "检查结果");
        fieldMap.put("explain", "结果说明");
        try {
            ExcelJXL.listToExcel(list, fieldMap, "清单", 65535, response);
        } catch (Exception e) {
            log.error("导出Excel失败。错误信息：" + e.getMessage());
        }
    }
}
